# -*- coding: utf-8 -*-
# @Time    : 2023/7/10 17:13
# @Author  : Hami

"""
数据规范化
"""
from flask import Flask, request, jsonify

app = Flask(__name__)
all_user = {"username": "hami", "password": "123456"}

# 提示信息：
# 登录成功
MSG_LOGIN_SUCCESS = {
    "msg": "登录成功",
    "code": 0,
    "data": {
        "id": "75",
        "username": "hami",
        "nickname": "哈米老师",
        "mobile": "",
        "email": "",
        "avatar": "http://shop-xo.hctestedu.com/static/upload/images/user_avatar/compr/2023/07/01/202307011819170015381852.jpg",
        "alipay_openid": "",
        "weixin_openid": "",
        "weixin_unionid": "",
        "weixin_web_openid": "",
        "baidu_openid": "",
        "toutiao_openid": "",
        "qq_openid": "",
        "qq_unionid": "",
        "integral": "420",
        "locking_integral": "0",
        "referrer": "0",
        "add_time": "1624022081",
        "add_time_text": "2021-06-18 21:14:41",
        "mobile_security": "",
        "email_security": "",
        "user_name_view": "hami",
        "is_mandatory_bind_mobile": 0,
        "token": "bd80726708856645618cfb2a5a6926d2"
    }
}
# 数据错误
MSG_DATA_NULL = {
    "msg": "用户名和密码不能为空",
    "code": -1,
    "data": ""
}
# 数据异常
MSG_DATA_ERROR = {
    "msg": "登录数据有误",
    "code": -1,
    "data": ""
}
# 用户名错误
MSG_ERROR_USERNAME = {
    "msg": "登录帐号不存在",
    "code": -3,
    "data": ""
}
# 密码错误
MSG_ERROR_PASSWORD = {
    "msg": "密码错误",
    "code": -4,
    "data": ""
}
# 登录失效
MSG_LOGIN_ERROR = {
    "msg": "登录失效，请检查当前登录用户。",
    "code": -5,
    "data": ""
}


# 1.主页设置多个路由
@app.route("/")
@app.route("/index.html")
def index():
    return "欢迎来后台管理系统"


# 2. 设置对应的请求方法，当未是POST请求则会报错
@app.route("/api/login", methods=["POST"], strict_slashes=False)
def login():
    # 以JSON数据获取数据
    res = request.get_json()

    # 获取用户名和密码
    try:
        username = res["username"]
        password = res["password"]
    except Exception:
        return jsonify(MSG_DATA_ERROR)
    else:
        """
        测试场景设计：
            1） 参数为空
            2） 登录成功
            3） 密码不正确
            4） 用户名不正确
            5） 参数填写错误
        """
        if username == "" or password == "":
            # return "参数不能为空"
            return jsonify(MSG_DATA_NULL)
        elif username == all_user["username"] and password == all_user["password"]:
            # return "登录成功"
            return jsonify(MSG_LOGIN_SUCCESS)
        elif username == all_user["username"] and password != all_user["password"]:
            # return "密码错误"
            return jsonify(MSG_ERROR_PASSWORD)
        elif username != all_user["username"]:
            # return "该用户名不存在"
            return jsonify(MSG_ERROR_USERNAME)
        else:
            # return "对应的参数填写有误"
            return jsonify(MSG_DATA_NULL)


@app.route("/api/info", methods=["GET"], strict_slashes=False)
def info():
    try:
        # 获取请求头
        token = request.headers.get("token")
        if token == MSG_LOGIN_SUCCESS["data"]["token"]:
            return jsonify(MSG_LOGIN_SUCCESS)
        else:
            return jsonify(MSG_LOGIN_ERROR)
    except Exception:
        return jsonify(MSG_LOGIN_ERROR)


if __name__ == '__main__':
    # app.run()
    # 调试模式：当有代码更新自动进行更新
    app.run(debug=True)
    # 通配符ip地址，并且设置端口号
    # app.run(debug=True, host="0.0.0.0", port=9000)
